import {Component, Input, OnDestroy, OnInit, ViewChild, ViewEncapsulation} from '@angular/core';
import {Router} from "@angular/router";
import {Subscription} from "rxjs/Subscription";
import {SeedInfiniteScroll} from "@shared/components/infinite-scroll/seed-infinite-scroll/seed-infinite-scroll.component";
import {Content} from "@shared/components/toolbar/toolbar-content";
import {ToolsService} from "@shared/service/tools/tools.service";
import {SponsorService} from "@sponsor/sponsor.service";

@Component({
  selector: 'seed-sponsor',
  templateUrl: './sponsor.component.html',
  styleUrls: ['./sponsor.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class SponsorComponent implements OnInit, OnDestroy {

  public filterObj = {
    areaList: [],
    endMonth: '',
    moneyValue: '',
    page: 1,
    rows: 15,
    proList: [],
    startMonth: ''
  };
  public moneyTotal = {
    moneyName: '全部',
    moneyValue: ''
  };
  public proListName = [];
  public areaName = [];
  public sponsorFilter = false;
  public sponsorList = [];
  public sponsorData = [];
  public filterData: any;
  subscription: Subscription = new Subscription();
  @ViewChild(SeedInfiniteScroll) infiniteScroll;
  @ViewChild(Content) content;

  constructor(public router: Router,
              public toolsService: ToolsService,
              public sponsorService: SponsorService) {

  }

  ngOnInit() {
    this.toolsService.showLoading();
    this.GetSponsorList();
    this.GetFilter();
  }

  onScroll() {
    setTimeout(() => {
      this.GetSponsorList();
    });
  }

  GetSponsorList() {
    this.subscription.add(
      this.sponsorService.getSponsorList(this.filterObj).subscribe(res => {
        if (res.result === '0') {
          this.filterObj.page++;
          if (res.data.hasNextPage === false) {
            this.infiniteScroll.enable(false);
          }
          this.sponsorData = res.data.list;
          this.infiniteScroll.complete();
          this.sponsorList = [...this.sponsorList, ...this.sponsorData];
          if (this.sponsorList.length === 0) {
            this.sponsorList = ['无'];
          }
          this.toolsService.hideLoading();
        }
      })
    );
  }

  GoToDetail(id) {
    this.router.navigate(['/sponsor/sponsor-detail'], {queryParams: {planId: id}});
  }

  ShowFilter() {
    this.sponsorFilter = true;
    this.content.scrollTop();
    this.content.disableScroll();
  }

  CloseFlter() {
    this.sponsorFilter = false;
    this.content.enableScroll(true);
  }

  GetFilter() {
    this.subscription.add(
      this.sponsorService.getFilterInfo().subscribe(res => {
        if (res.result === '0') {
          this.filterData = res.data;
          this.filterData.moneyList = [this.moneyTotal, ...this.filterData.moneyList];
        }
      })
    );
  }

  Filter(filterParam) {
    this.filterObj = {...filterParam.filterObj};
    this.sponsorList = [];
    this.proListName = filterParam.proListName;
    this.areaName = filterParam.areaName;
    this.GetSponsorList();
  }

  ngOnDestroy() {
    this.subscription.unsubscribe();
  }
}
